package gold.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/20.
 */
public class InputUtil<T> {

    public static <T> T[][] toDoubleArray(List<List<T>> ddList) {
        int rows = ddList.size(), cols = ddList.get(0).size();
        T[][] resDoubleDimensionArray = (T[][]) Array.newInstance(new InputUtil<T>().getTClass(), rows, cols);

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                resDoubleDimensionArray[i][j] = ddList.get(i).get(j);
            }
        }

        return resDoubleDimensionArray;
    }

    public Class<T> getTClass() {
        Class<T> tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return tClass;
    }

    public <T> List<T> twoDArrayToList(T[][] twoDArray) {
        List<T> list = new ArrayList<T>();
        for (T[] array : twoDArray) {
            list.addAll(Arrays.asList(array));
        }
        return list;
    }

    public static int[] toIntegerArray(String input) {
        if ("[]".equals(input) || "[ ]".equals(input)) return new int[0];
        String[] strArray = input.substring(0, input.indexOf("]") == -1 ? input.length() : input.indexOf("]")).
                substring(input.indexOf("[") + 1).split(",");
        int[] resArray = new int[strArray.length];
        try {
            for (int i = 0; i < strArray.length; i++) {
                resArray[i] = Integer.parseInt(strArray[i].trim());
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return resArray;
    }

    public static char[] toCharArray(String input) {
        String[] charList = input.split("\",");
        char[] charArray = new char[charList.length];
        int i = 0;
        for (String ch : charList) {
            if (ch.endsWith("\"]")) ch = ch.substring(0, ch.length() - 2);
            charArray[i++] = ch.charAt(ch.length() - 1);
        }

        return charArray;
    }

    public static String[] toStringArray(String input) {
        String[] strList = input.split("\",");
        String[] stringArray = new String[strList.length];
        int i = 0;
        for (String ch : strList) {
            if (ch.endsWith("\"]")) ch = ch.replaceAll("\"]", "");
            if (ch.startsWith("[\"")) ch = ch.replaceAll("\\[\"", "");
            if (ch.startsWith("\"")) ch = ch.replaceAll("\"", "");
            stringArray[i++] = ch;
        }

        return stringArray;
    }

    public static char[][] toDoubleCharArray(String input) {
        String[] lineList = input.split(",\\[");
        char[][] charArray = new char[lineList.length][lineList[0].split(",").length];
        int lineCount = 0;
        for (String line : lineList) {
            String[] lineEle = line.split(",");
            int eleCount = 0;
            for (String ele : lineEle) {
                ele = ele.split("\"")[1];
                charArray[lineCount][eleCount++] = ele.charAt(0);
            }
            ++lineCount;
        }

        return charArray;
    }

    public static String[][] toDoubleStringArray(String input) {
        input = skipWhiteSpace(input);
        String[] lineList = input.split(",\\[");
        int maxLineLength = 0;
        for (String line : lineList) {
            maxLineLength = Math.max(maxLineLength, line.split(",").length);
        }

        String[][] strArray = new String[lineList.length][maxLineLength];
        int lineCount = 0;
        for (String line : lineList) {
            String[] lineEle = line.split(",");
            int eleCount = 0;
            for (String ele : lineEle) {
                ele = ele.split("\"")[1];
                strArray[lineCount][eleCount++] = ele;
            }
            ++lineCount;
        }

        return strArray;
    }

    public static List<List<String>> toDoubleStringList(String input) {
        String[][] res = toDoubleStringArray(input);
        List<List<String>> doubleStrList = new ArrayList<>();
        for (String[] re : res) {
            doubleStrList.add(Arrays.asList(re));
        }

        return doubleStrList;
    }

    public static int[][] toDoubleIntegerArray(String input) {
        String[] strArray = input.substring(0, input.lastIndexOf("]")).substring(input.indexOf("[") + 1).split("\\],");
        int[][] resArray = new int[strArray.length][];
        for (int i = 0; i < strArray.length; i++) {
            resArray[i] = toIntegerArray(strArray[i]);
        }

        return resArray;
    }

    public static List<List<Integer>> toDoubleIntegerList(String input) {
        List<List<Integer>> resList = new ArrayList<>();

        int[][] doubleIntegerArray = toDoubleIntegerArray(input);
        for (int[] line : doubleIntegerArray) {
            List<Integer> curLine = new ArrayList<>();
            for (int num : line) {
                curLine.add(num);
            }
            resList.add(curLine);
        }

        return resList;
    }

    public static int[] readLongArrayFromFile(String fileName) {
        List<Integer> res = new ArrayList<>();
        try {
            File file = new File("/Users/zhfan/Downloads/" + fileName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] elementList = line.split(",");
                for (String element : elementList) {
                    StringBuilder elementBuilder = new StringBuilder(element);
                    if (elementBuilder.indexOf("[") >= 0) elementBuilder.deleteCharAt(elementBuilder.indexOf("["));
                    if (elementBuilder.indexOf("]") >= 0) elementBuilder.deleteCharAt(elementBuilder.indexOf("]"));
                    res.add(Integer.parseInt(elementBuilder.toString()));
                }
            }

        } catch (Exception e) {
            System.out.println("error read file");
        }

        int[] resArray = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            resArray[i] = res.get(i);
        }

        return resArray;
    }

    public static String skipWhiteSpace(String input) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) != ' ') sb.append(String.valueOf(input.charAt(i)));
        }
        return sb.toString();
    }

    public static void run() {
        ;
    }

    public static void main(String[] args) {
        String str = "[[\"1\",\"0\",\"1\",\"1\",\"0\",\"1\"],[\"1\",\"1\",\"1\",\"1\",\"1\",\"1\"],[\"0\",\"1\",\"1\",\"0\",\"1\",\"1\"],[\"1\",\"1\",\"1\",\"0\",\"1\",\"0\"],[\"0\",\"1\",\"1\",\"1\",\"1\",\"1\"],[\"1\",\"1\",\"0\",\"1\",\"1\",\"1\"]]";
        System.out.println(toDoubleCharArray(str));

        String charArray = "[\"A\",\"A\",\"A\",\"B\",\"B\",\"B\"]";

        String strDoubleArray = "[[\"JFK\",\"SFO\"],[\"JFK\",\"ATL\"],[\"SFO\",\"ATL\"],[\"ATL\",\"JFK\"],[\"ATL\",\"SFO\"]]";
        System.out.println(toDoubleStringArray(strDoubleArray));


        String strSingleArray = "[\"abcd\",\"dcba\",\"lls\",\"s\",\"sssll\"]";
        System.out.println(Arrays.toString(toStringArray(strSingleArray)));
    }


}
